﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using CharacterTrailClient.Binding;
using CharacterTrailClient.Style;
using CharacterTrailClient.Windows;
using CharacterTrailLibrary.CustomModels;
using CharacterTrailLibrary.Factory;
using CharacterTrailLibrary.Interface;
using CharacterTrailLibrary.Persist.BLL.Model;
using DevExpress.XtraEditors;
using DevExpress.XtraSpellChecker.Native;

namespace CharacterTrailClient.Controls
{
    public partial class ProductTree2 : DevExpress.XtraEditors.XtraUserControl
    {
        private readonly IBom _bomImpl = InstanceFactory.GetBom();
        public ProductTree2()
        {
            InitializeComponent();
            if (GlobalClass.RunTimeMode)
            {
                DataBinder.BindingBomMainItem2(this.gridLookUpEditProductItemNumber, false);
                LoadStaticData();
                gridLookUpEditProductItemNumber.EditValueChanged += new EventHandler(GridLookUpEditProductItemNumberEditValueChanged);
            }
            this.treeListMain.CustomDrawNodeCell += TreeListMainCustomDrawNodeCell;
        }

        private void TreeListMainCustomDrawNodeCell(object sender, DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs e)
        {
            try
            {
                var model = (ProductItemModel)this.treeListMain.GetDataRecordByNode(e.Node);
                if (model != null)
                {
                    if (model.CheckStateDesc == ProductItemModel.CheckStateYes)
                    {
                        e.Appearance.ForeColor = Color.Blue;
                    }
                    else if (model.CheckStateDesc == ProductItemModel.CheckStateNo)
                    {
                        e.Appearance.ForeColor = Color.Red;
                    }
                }
            }
            catch (Exception exception)
            {
                Message.MsgBox.ShowError(exception.Message);
            }
        }

        private void LoadStaticData()
        {
            gridLookUpEditProductItemNumber.Properties.DataSource =
                _bomImpl.GetBomMainModelsByCondition(" 59 = 59");
        }

        private void GridLookUpEditProductItemNumberEditValueChanged(object sender, EventArgs e)
        {
            try
            {
                var model = (TBomMainModel)this.gridLookUpEditProductItemNumber.Properties.GetRowByKeyValue(gridLookUpEditProductItemNumber.EditValue);
                if (model != null)
                {
                    this.textEditProductItemName.Text = model.ProductItemName;
                    LoadTreeList(model);
                }
            }
            catch (Exception exception)
            {
                Message.MsgBox.ShowError(exception.Message);
            }
        }

        private void LoadTreeList(TBomMainModel bomMainModel)
        {
            try
            {
                var list = _bomImpl.BuildProductTree2(bomMainModel.ProductItemId, bomMainModel.ProductVersion);
                this.treeListMain.DataSource = list;
                //RowStyleConditionController.AddStyleFormatCondition4TreeList(this.treeListMain, "CheckStateDesc",
                //                                                             String.Format("[CheckStateDesc] =='{0}'",ProductItemModel.CheckStateYes), Color.Blue);
                //RowStyleConditionController.AddStyleFormatCondition4TreeList(this.treeListMain, "CheckStateDesc",
                //                                                           String.Format("[CheckStateDesc] =='{0}'", ProductItemModel.CheckStateNo), Color.Red);
                this.treeListMain.ExpandAll();
            }
            catch (Exception exception)
            {
                Message.MsgBox.ShowError(exception.Message);
            }
        }

        private void CmdQuery_Click(object sender, EventArgs e)
        {
            LoadStaticData();
        }

        private void CmdEditBom_Click(object sender, EventArgs e)
        {
            try
            {
                var node = (ProductItemModel)this.treeListMain.GetDataRecordByNode(this.treeListMain.FocusedNode);
                if (node != null && !node.IsLeafNode)
                {
                    var bomImpl = InstanceFactory.GetBom();
                    var model = bomImpl.GetBomMainModel(node.ProductItemId, node.ProductVersion);
                    if (model != null)
                    {
                        var form = new FormBomMainEdit(model);
                        form.SetNewButtonEnable(false);
                        form.ShowDialog(this);

                        var model2 = (TBomMainModel)this.gridLookUpEditProductItemNumber.Properties.GetRowByKeyValue(gridLookUpEditProductItemNumber.EditValue);
                        if (model2 != null)
                        {
                            this.textEditProductItemName.Text = model2.ProductItemName;
                            LoadTreeList(model2);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Message.MsgBox.ShowError(exception.Message);
            }
        }
    }
}
